package com.fc.test.controller.gen;

import com.fc.test.common.base.BaseController;
import com.fc.test.common.domain.AjaxResult;
import com.fc.test.model.auto.PaperSheet;
import com.fc.test.model.auto.PaperSheetOrPaperRecord;
import com.fc.test.model.auto.TsysDatas;
import com.fc.test.model.custom.TableSplitResult;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.model.custom.TitleVo;
import com.fc.test.service.PaperSheetService;
import com.fc.test.service.SysDatasService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

@Api(value = "票据表")
@Controller
@RequestMapping("/PaperSheetController")
public class PaperSheetController extends BaseController {

    private String prefix = "gen/paperSheet";
    @Autowired
    private PaperSheetService paperSheetService;
    @Autowired
    private SysDatasService sysDatasService;

    /**
     * 分页跳转
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/view")
    @RequiresPermissions("gen:paperSheet:view")
    public String view(ModelMap model) {
        String str = "票据表";
        setTitle(model, new TitleVo("列表", str + "管理", true, "欢迎进入" + str + "页面", true, false));
        return prefix + "/list";
    }

    /**
     * 分页查询
     */
    //@Log(title = "票据表集合查询", action = "111")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/list")
    @RequiresPermissions("gen:paperSheet:list")
    @ResponseBody
    public Object list(Tablepar tablepar, PaperSheet record) {
        PageInfo<PaperSheet> page = paperSheetService.list(tablepar, record);
        TableSplitResult<PaperSheet> result = new TableSplitResult<PaperSheet>(page.getPageNum(), page.getTotal(), page.getList());
        return result;
    }

    /**
     * 新增跳转
     */
    @ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        return prefix + "/add";
    }

    /**
     * 新增
     */
    //@Log(title = "票据表新增", action = "111")
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/add")
    @RequiresPermissions("gen:paperSheet:add")
    @ResponseBody
    public AjaxResult add(PaperSheet paperSheet) {
        int b = paperSheetService.insertSelective(paperSheet);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    //@Log(title = "票据表删除", action = "111")
    @ApiOperation(value = "删除", notes = "删除")
    @PostMapping("/remove")
    @RequiresPermissions("gen:paperSheet:remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int b = paperSheetService.deleteByPrimaryKey(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 检查Name
     *
     * @param
     * @return
     */
    @ApiOperation(value = "检查Name唯一", notes = "检查Name唯一")
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public int checkNameUnique(PaperSheet paperSheet) {
        int b = paperSheetService.checkNameUnique(paperSheet);
        if (b > 0) {
            return 1;
        } else {
            return 0;
        }
    }


    /**
     * 修改跳转
     *
     * @param id
     * @param mmap
     * @return
     */
    @ApiOperation(value = "修改跳转", notes = "修改跳转")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("PaperSheet", paperSheetService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    //@Log(title = "票据表修改", action = "111")
    @ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("gen:paperSheet:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PaperSheet record) {
        return toAjax(paperSheetService.updateByPrimaryKeySelective(record));
    }


    /**
     * 根据主键查询单据
     *
     * @param id
     * @param
     * @return
     */
    @ApiOperation(value = "根据id查询唯一", notes = "根据id查询唯一")
    @PostMapping("/get/{id}")
    public PaperSheet edit(@PathVariable("id") String id) {
        return paperSheetService.selectByPrimaryKey(id);
    }

    /**
     * 根据主键查询单据和消费记录
     *
     * @param id
     * @param
     * @return
     */
    @ApiOperation(value = "根据主键查询单据和消费记录", notes = "根据主键查询单据和消费记录")
    @PostMapping("/getPaperSheetOrPaperRecord/{id}")
    public PaperSheetOrPaperRecord getPaperSheetOrPaperRecord(@PathVariable("id") String id) {
        return paperSheetService.queryPaperSheetOrPaperRecord(id);
    }


    /**
     * 通过票据单号查询二维码
     * 2020年10月28日16:42:49 jyl
     */
    @ApiOperation(value = "通过票据单号查询二维码", notes = "通过票据单号查询二维码")
    @RequestMapping(value = "/getQrCode")
    public void getImage(@ApiParam(name = "id", value = "id") @RequestParam(value = "id") String id, HttpServletRequest request, HttpServletResponse response) {
        FileInputStream fis = null;
        response.setContentType("image/gif");
        try {
            OutputStream out = response.getOutputStream();
            TsysDatas tsysDatas = sysDatasService.selectByPrimaryKey(id);
            File file = new File(tsysDatas.getFilePath());
            fis = new FileInputStream(file);
            byte[] b = new byte[fis.available()];
            fis.read(b);
            out.write(b);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
